package com.sugar.queryinfo.module.business.controller.admin.data.dataqueryxfhx;

import com.sugar.queryinfo.module.business.controller.admin.data.dataqueryxfhx.vo.DataQueryXfhxPageReqVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataqueryxfhx.vo.DataQueryXfhxRespVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataqueryxfhx.vo.DataQueryXfhxSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.dal.dataobject.dataqueryxfhx.DataQueryXfhxDO;
import com.sugar.queryinfo.module.business.service.dataqueryxfhx.DataQueryXfhxService;

@Tag(name = "管理后台 - 数据查询-逾期探查报告")
@RestController
@RequestMapping("/business/data-query-xfhx")
@Validated
public class DataQueryXfhxController {

    @Resource
    private DataQueryXfhxService dataQueryXfhxService;

    @PostMapping("/create")
    @Operation(summary = "创建数据查询-逾期探查报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:create')")
    public CommonResult<Long> createDataQueryXfhx(@Valid @RequestBody DataQueryXfhxSaveReqVO createReqVO) {
        return success(dataQueryXfhxService.createDataQueryXfhx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据查询-逾期探查报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:update')")
    public CommonResult<Boolean> updateDataQueryXfhx(@Valid @RequestBody DataQueryXfhxSaveReqVO updateReqVO) {
        dataQueryXfhxService.updateDataQueryXfhx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据查询-逾期探查报告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:delete')")
    public CommonResult<Boolean> deleteDataQueryXfhx(@RequestParam("id") Long id) {
        dataQueryXfhxService.deleteDataQueryXfhx(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据查询-逾期探查报告")
                @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:delete')")
    public CommonResult<Boolean> deleteDataQueryXfhxList(@RequestParam("ids") List<Long> ids) {
        dataQueryXfhxService.deleteDataQueryXfhxListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据查询-逾期探查报告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:query')")
    public CommonResult<DataQueryXfhxRespVO> getDataQueryXfhx(@RequestParam("id") Long id) {
        DataQueryXfhxDO dataQueryXfhx = dataQueryXfhxService.getDataQueryXfhx(id);
        return success(BeanUtils.toBean(dataQueryXfhx, DataQueryXfhxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据查询-逾期探查报告分页")
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:query')")
    public CommonResult<PageResult<DataQueryXfhxRespVO>> getDataQueryXfhxPage(@Valid DataQueryXfhxPageReqVO pageReqVO) {
        PageResult<DataQueryXfhxDO> pageResult = dataQueryXfhxService.getDataQueryXfhxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataQueryXfhxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据查询-逾期探查报告 Excel")
    @PreAuthorize("@ss.hasPermission('business:data-query-xfhx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataQueryXfhxExcel(@Valid DataQueryXfhxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataQueryXfhxDO> list = dataQueryXfhxService.getDataQueryXfhxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据查询-逾期探查报告.xls", "数据", DataQueryXfhxRespVO.class,
                        BeanUtils.toBean(list, DataQueryXfhxRespVO.class));
    }

}